//
//  ViewController.h
//  lib
//

//
#import "ViewBase.h"
#import "ViewController.h"
#import "libCommon.h"




@interface ViewController(private)
- (void)initMember;
- (void)releaseMember;
@end

@implementation ViewController
@synthesize m_BaseView;
@synthesize m_DeviceOrie;
@synthesize m_IsRotating;



- (void)initMember
{
	m_BaseView = nil;
	
	m_DeviceOrie = UIDeviceOrientationUnknown;
	
	m_LockStartTime = TIME_INIT;
	m_LockTime = TIME_INIT;
	m_pLockSenderObj = nil;
	m_IsRotating = NO;
}

- (id)init
{
	self = [super init];
	if( self ) {
		[self initMember];
		
		
		UIDevice* device = [UIDevice currentDevice];
		m_DeviceOrie = device.orientation;
		
		
		m_BaseView = [[ViewBase alloc] initWithFrame:DISPLAY_RECT_PORT];		
		m_BaseView.autoresizingMask = VIEW_AUTORESIZE_ALL;
		CGRect viewFrame= DISPLAY_RECT_PORT;
		viewFrame.origin.y += CLOCK_HEIGHT;
		[self.view setFrame:viewFrame];
		[self.view addSubview:m_BaseView];
		[self.view addSubview:m_BaseView];
	}
	return self;
}



- (void)releaseMember
{
	SAFE_RELEASE(m_BaseView)
}

- (void)dealloc
{
	[self releaseMember];
	
	[super dealloc];
}



- (void)LockRotate:(id)sender lockTime:(double)lock_time
{
	m_pLockSenderObj = sender;
	m_LockTime = lock_time;
	m_LockStartTime = CFAbsoluteTimeGetCurrent();
}

- (BOOL)UnlockRotate:(id)sender
{
	BOOL ret = YES;
	double now_time = CFAbsoluteTimeGetCurrent();
	
	if ( m_pLockSenderObj ) {
		if (( sender == m_pLockSenderObj )&&( (now_time-m_LockStartTime) >= m_LockTime )) {
			m_pLockSenderObj = nil;
			m_LockTime = m_LockStartTime = TIME_INIT;
		} else {
			ret = NO;
		}
	}
	return ret;
}



- (void)update
{
	[m_BaseView update];
}



- (void)addSubview:(UIView*)view
{
	[m_BaseView addSubview:view];
}
//-------------------------------------------------------------------
// View排除
- (void)removeSubview:(UIView*)view
{
	removeViewOnView( m_BaseView, view );
}

- (void)removeSubviewAll
{
	removeViewAllOnView( m_BaseView );
}

- (void)replaceSubView:(UIView*)new_view oldView:(UIView*)old_view
{
	replaceView( m_BaseView, new_view, old_view );
}



#pragma mark -
#pragma mark UIViewController delegates

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	BOOL ret = NO;
	
	
	if( !m_pLockSenderObj )
	{
		ret = [m_BaseView shouldAutorotate:interfaceOrientation];
		if ( ret ) {
			m_DeviceOrie = interfaceOrientation;
		}
	}
	return ret;
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
	m_IsRotating = YES;
	
	NSNotification* notification = [NSNotification notificationWithName:DEVICE_WILLROTATE_NOTIFICATION object:nil];
	[[NSNotificationCenter defaultCenter] postNotification:notification];
	
	[m_BaseView willRotate:toInterfaceOrientation duration:duration];
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
	m_IsRotating = NO;
	
	NSNotification* notification = [NSNotification notificationWithName:DEVICE_DIDROTATE_NOTIFICATION object:nil];
	[[NSNotificationCenter defaultCenter] postNotification:notification];
	
	[m_BaseView didRotate:fromInterfaceOrientation];
}
//////////////////////////////////////////////////////////////////////
#pragma mark -

@end
